package com.park.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.JobSheet;
import com.park.model.Paging;
import com.park.service.JobSheetService;
import com.park.service.Utility;


@Controller
@RequestMapping("jobsheet")
public class JobSheetController {
	@Autowired
	private JobSheetService jobSheetService;
	
	private static Log logger = LogFactory.getLog(JobSheetController.class);
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@RequestMapping(value="/getsearchJobSheet",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String  getsearchJobSheet(@RequestBody Map<String,Object> args) {
		int startPage = (int)args.get("startPage"); 
		int pageSize = (int)args.get("pageSize");
		String deviceType = (String)args.get("devicetype");
		int parkId = (int)args.get("parkId");
		Map<String, Object> ret = new HashMap<String, Object>();
		List<JobSheet> jobsheet=jobSheetService.getjobsheetPage((startPage - 1) * pageSize, pageSize,deviceType,parkId);
		int allcount=jobSheetService.getjobsheetCount(parkId, deviceType);
		Paging page = new Paging(startPage, pageSize, allcount, jobsheet);
		ret.put("status", 1001);
		ret.put("message", "get jobsheet success");
		ret.put("body", page);
		return Utility.gson.toJson(ret);
	}
	
	@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insert(@RequestBody JobSheet jobSheet){
		logger.info("工单新增操作");
		Map<String, Object> result=new HashMap<>();
		int num=jobSheetService.insert(jobSheet);
		logger.info("工单新增时间:"+df.format(new Date()));
		if(num==1){
			result.put("status", 1001);
			result.put("message", "success");
			logger.info("工单新增成功!");
		}else{
			result.put("status", 1002);
			result.put("message", "failde");
			logger.info("工单新增失败!");
		}
		return Utility.gson.toJson(result);
	}
}
